package demo.proxy.dyn.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author dingwen
 * 2021.05.25 11:57
 */
public class MyInvocationHandler implements InvocationHandler {


    Object object;

    /**
     * 获得代理接口
     *
     * @param object 对象
     * @return {@link Object}
     */
    public Object getProxy(Object object) {
        this.object = object;
        return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        doBefore();
        Object result = method.invoke(object, args);
        doAfter();
        return result;
    }

    /**
     * 前置处理
     */
    public void doBefore(){
        System.out.println("前置处理");
    }

    /**
     * 后置处理
     */
    public void doAfter(){
        System.out.println("后置处理");
    }
}
